﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SerialPorts;
using DataCollector.Entity;
using SilverlightApp.data;
using SilverlightApp.Web.DataBase;

namespace EnergyControl_SeverDlg
{
    public partial class EnergyControl_sever : Form
    {
        public EnergyControl_sever()
        {
            InitializeComponent();
        }

        private SerialPortHelper sph = new SerialPortHelper();
        private System.Collections.Queue msgQueue;
        private bool portOpened = false;

        private void Form1_Load(object sender, EventArgs e)
        {
            MaximumSize = MinimumSize = this.Size;

            cmbSerialPortNm.Items.Clear();

            string[] portNames = sph.GetCommPortNames();
            cmbSerialPortNm.Items.AddRange(portNames);
            cmbSerialPortNm.SelectedIndex = 0;
            sph.MonitorAction = OnMonitorAction;
            msgQueue = new System.Collections.Queue();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Enabled = false;
        }

        private void btnOpenSerial_Click(object sender, EventArgs e)
        {

            try
            {
                if (!portOpened)
                {
                    var portName = cmbSerialPortNm.SelectedItem.ToString();
                    StartMonitor(portName);
                    cmbSerialPortNm.Enabled = false;
                    btnOpenSerial.Text = "关闭串口";
                }
                else
                {
                    EndMonitor();
                    cmbSerialPortNm.Enabled = true;
                    btnOpenSerial.Text = "打开串口";
                }
                portOpened = !portOpened;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        FileWriter fw = new FileWriter();
        DataClassesForInsertDataContext dc = new DataClassesForInsertDataContext();
        MonitorDataDao mdd = new MonitorDataDao(); 
        void timer1_Tick(object sender, EventArgs e)
        {
            lock (msgQueue.SyncRoot)
            {
                while (msgQueue.Count > 0)
                {
                    MonitorResult res = msgQueue.Dequeue() as MonitorResult;
                    string msg = "温度：" + res.Temperature + " 湿度：" + res.Humidity + " 光照：" + res.Illumination;
                    string file1 = "c:\\file1.txt";
                    string file2 = "c:\\file2.txt";
                    //mdd.Insert("hangzhou_001", res);
                    fw.Write(file1, file2, res);
                    richTextBox1.AppendText(msg + Environment.NewLine);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseWindow();
        }

        private void CloseWindow()
        {
            sph.Close();
            this.Close();
        }

        private void EnergyControl_sever_FormClosing(object sender, FormClosingEventArgs e)
        {
            sph.Close();
        }
    }
}
